#!/bin/bash

case $1 in
    install_prepare)
        wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
        sudo dpkg -i packages-microsoft-prod.deb

        sudo apt update
        sudo apt install -y build-essential curl libssl-dev pkg-config build-essential git-lfs cmake  dotnet-sdk-8.0 dotnet-runtime-8.0 lrzsz

        rm -rf packages-microsoft-prod.deb

        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

        openssl rand -hex 32 | tr -d '\n' > /home/ubuntu/jwtsecret
    ;;
    install_nimbus)
        if [ $# -lt 2 ]
        then
          echo "参数错误, 格式如下: "
          echo "pf install_nimbus USE_RAM(推荐虚拟机8G内存用4, 16G用8)"
          exit 1
        fi
        cd ~
        rm -rf nimbus-eth2
        git clone --depth 1 https://github.com/status-im/nimbus-eth2
        cd nimbus-eth2
        make -j${2} nimbus_beacon_node
        build/nimbus_beacon_node trustedNodeSync --network:holesky --data-dir=build/data/shared_holesky_0 --trusted-node-url=https://holesky-checkpoint-sync.stakely.io/
    ;;
    start_nimbus|sn)
        wallet=$2
        if [ $# -lt 2 ]
        then
            if [ -e "/home/ubuntu/wallet.txt" ]; then
                wallet=$(cat /home/ubuntu/wallet.txt)
                echo "使用钱包地址: ${wallet}"
            else
                echo "- 请输入命令: pf run_nimbus 钱包地址"
                echo "- 或将钱包地址保存在/home/ubuntu/wallet.txt, 并执行pf run_nimbus"
                exit 1
            fi
        fi
        cd ~/nimbus-eth2

        # 启动命令
        ./run-holesky-beacon-node.sh --web3-url=http://127.0.0.1:8551 --suggested-fee-recipient=${wallet}  --jwt-secret=/home/ubuntu/jwtsecret --enr-auto-update=true
    ;;

    install_coral)
      cd ~
      rm -rf puffer
      mkdir puffer
      cd puffer
      git clone https://github.com/PufferFinance/coral
      cd coral
      cargo build --release
      echo 'you' >  password.txt
      echo "需手动前往=>  cd ~/puffer/coral 后, 运行 Coral命令"
    ;;
    after_install_coral)
      cd ~/puffer/coral/etc/keys/bls_keys
      file=$(ls -p "./" | grep -v /)
      rm -rf ~/nimbus-eth2/validator_keys/
      mkdir ~/nimbus-eth2/validator_keys/
      cp -v $file ~/nimbus-eth2/validator_keys/keystore.json

      cd ~/nimbus-eth2
      echo "请输入密码: you"
      ./build/nimbus_beacon_node deposits import  --data-dir=build/data/shared_holesky_0
    ;;


    validator_prepare)
        cd ~
        if test -d "/home/ubuntu/nethermind/src/Nethermind/"; then
            echo "已有下载文件"
        else
            git clone --depth 1 https://github.com/NethermindEth/nethermind.git
            if test -d "/home/ubuntu/nethermind/src/Nethermind/"; then
                echo "下载成功"
            else
                echo "下载失败, 请重试"
                exit 1
            fi
            cd nethermind/src/Nethermind/
            dotnet build Nethermind.sln -c Release
        fi
    ;;
    start_validator|sv)
        cd ~/nethermind/src/Nethermind/Nethermind.Runner

        dotnet run -c Release --   --config=holesky   --datadir="../../../../nethermind-datadir"   --JsonRpc.Host=0.0.0.0   --JsonRpc.JwtSecretFile=/home/ubuntu/jwtsecret
    ;;

    *)
        echo "unknown command..."
        echo "         更新脚本:\t pf_update"
        echo "   运行numbus节点:\t pf sn"
        echo "运行validator节点:\t pf sv"
    ;;
esac